package com.jinjiafeng.framelibrary.http;

import com.jinjiafeng.baselibrary.db.DaoSupportFactory;
import com.jinjiafeng.baselibrary.db.IDaoSupport;
import com.jinjiafeng.baselibrary.db.crud.QuerySupport;
import com.jinjiafeng.baselibrary.util.MD5Util;

import java.util.List;

/**
 * @author jinjiafeng
 *         Time :18-5-8
 */

public class CacheDataUtil {

    public static String getCacheData(String url) {
        final QuerySupport<CacheData> support = DaoSupportFactory.getFactory()
                .getDao(CacheData.class).querySupport();
        final List<CacheData> cacheDataList = support.selection("urlKey=?")
                .selectionArgs(MD5Util.string2MD5(url)).query();
        if (cacheDataList != null && cacheDataList.size() > 0) {
            return cacheDataList.get(0).getResultJson();
        }
        return "";

    }

    /**
     * 缓存数据
     */
    public static long cacheData(String finalUrl, String resultJson) {
        final IDaoSupport<CacheData> dataDaoSupport = DaoSupportFactory.getFactory().
                getDao(CacheData.class);
        dataDaoSupport.delete("urlKey=?", MD5Util.string2MD5(finalUrl));
        return dataDaoSupport.insert(new CacheData(MD5Util.string2MD5(finalUrl), resultJson));
    }
}
